﻿#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    writeNetworkConfigToXml();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::writeNetworkConfigToXml()
{
    // 创建 QDomDocument 对象
    QDomDocument doc;

    // 创建 XML 根节点
    QDomElement root = doc.createElement("configuration");
    doc.appendChild(root);

    // 创建 network 节点
    QDomElement networkElement = doc.createElement("network");
    root.appendChild(networkElement);

    // 创建 udpA_softUnicast 节点
    QDomElement udpA_softUnicastElement = doc.createElement("udpA_softUnicast");
    networkElement.appendChild(udpA_softUnicastElement);
    QDomElement udpA_softUnicastHost = doc.createElement("host");
    udpA_softUnicastHost.appendChild(doc.createTextNode("127.0.0.1"));
    udpA_softUnicastElement.appendChild(udpA_softUnicastHost);
    QDomElement udpA_softUnicastPort = doc.createElement("port");
    udpA_softUnicastPort.appendChild(doc.createTextNode("3333"));
    udpA_softUnicastElement.appendChild(udpA_softUnicastPort);

    // 创建 udpA_softMulticast 节点
    QDomElement udpA_softMulticastElement = doc.createElement("udpA_softMulticast");
    networkElement.appendChild(udpA_softMulticastElement);
    QDomElement udpA_softMulticastHost = doc.createElement("host");
    udpA_softMulticastHost.appendChild(doc.createTextNode("224.0.0.1"));
    udpA_softMulticastElement.appendChild(udpA_softMulticastHost);
    QDomElement udpA_softMulticastPort = doc.createElement("port");
    udpA_softMulticastPort.appendChild(doc.createTextNode("2222"));
    udpA_softMulticastElement.appendChild(udpA_softMulticastPort);

    // 创建 udpA_softBroadcast 节点
    QDomElement udpA_softBroadcastElement = doc.createElement("udpA_softBroadcast");
    networkElement.appendChild(udpA_softBroadcastElement);
    QDomElement udpA_softBroadcastHost = doc.createElement("host");
    udpA_softBroadcastHost.appendChild(doc.createTextNode("255.255.255.255"));
    udpA_softBroadcastElement.appendChild(udpA_softBroadcastHost);
    QDomElement udpA_softBroadcastPort = doc.createElement("port");
    udpA_softBroadcastPort.appendChild(doc.createTextNode("4444"));
    udpA_softBroadcastElement.appendChild(udpA_softBroadcastPort);

    // 创建 udpB_softUnicast 节点
    QDomElement udpB_softUnicastElement = doc.createElement("udpB_softUnicast");
    networkElement.appendChild(udpB_softUnicastElement);
    QDomElement udpB_softUnicastHost = doc.createElement("host");
    udpB_softUnicastHost.appendChild(doc.createTextNode("127.0.0.1"));
    udpB_softUnicastElement.appendChild(udpB_softUnicastHost);
    QDomElement udpB_softUnicastPort = doc.createElement("port");
    udpB_softUnicastPort.appendChild(doc.createTextNode("12300"));
    udpB_softUnicastElement.appendChild(udpB_softUnicastPort);

    // 创建 udpB_softMulticast 节点
    QDomElement udpB_softMulticastElement = doc.createElement("udpB_softMulticast");
    networkElement.appendChild(udpB_softMulticastElement);
    QDomElement udpB_softMulticastHost = doc.createElement("host");
    udpB_softMulticastHost.appendChild(doc.createTextNode("224.0.0.1"));
    udpB_softMulticastElement.appendChild(udpB_softMulticastHost);
    QDomElement udpB_softMulticastPort = doc.createElement("port");
    udpB_softMulticastPort.appendChild(doc.createTextNode("12345"));
    udpB_softMulticastElement.appendChild(udpB_softMulticastPort);

    // 创建 udpB_softBroadcast 节点
    QDomElement udpB_softBroadcastElement = doc.createElement("udpB_softBroadcast");
    networkElement.appendChild(udpB_softBroadcastElement);
    QDomElement udpB_softBroadcastHost = doc.createElement("host");
    udpB_softBroadcastHost.appendChild(doc.createTextNode("255.255.255.255"));
    udpB_softBroadcastElement.appendChild(udpB_softBroadcastHost);
    QDomElement udpB_softBroadcastPort = doc.createElement("port");
    udpB_softBroadcastPort.appendChild(doc.createTextNode("10000"));
    udpB_softBroadcastElement.appendChild(udpB_softBroadcastPort);

    // 创建 tcp 节点
    QDomElement tcpElement = doc.createElement("tcp");
    networkElement.appendChild(tcpElement);
    QDomElement tcpHost = doc.createElement("host");
    tcpHost.appendChild(doc.createTextNode("127.0.0.1"));
    tcpElement.appendChild(tcpHost);
    QDomElement tcpPort = doc.createElement("port");
    tcpPort.appendChild(doc.createTextNode("8010"));
    tcpElement.appendChild(tcpPort);
    // 保存 XML 到文件
    QFile file("config.xml");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qDebug() << "Failed to open file for writing.";
        return;
    }

    QTextStream out(&file);
    doc.save(out, 4);  // 保存 XML 文档，4 表示缩进级别
    file.close();

    qDebug() << "XML configuration written to config.xml.";
}
